//
//  HelpWebViewController.swift
//  AutoPrint
//
//  Created by David on 2017/1/15.
//  Copyright © 2017年 fatcat. All rights reserved.
//

import UIKit

class HelpWebViewController: UIViewController, UIWebViewDelegate {

    var webURL: NSURL!
    
    convenience init(title: String, url: NSURL){
        self.init()
        self.title = title
        webURL = url
    }
    
    var webView: UIWebView!
    var progressBar: UIProgressView!
//    var toolbar: UIToolbar!
    var backToolbar: UIToolbar!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        
        webView = UIWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
        webView.delegate = self
        webView.scalesPageToFit = true
        webView.loadRequest(NSURLRequest(URL: webURL))
        view.addSubview(webView)
        
        progressBar = UIProgressView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: 0))
        view.addSubview(progressBar)
        
//        toolbar = UIToolbar(frame: CGRect(x: 0, y: view.frame.height - 44, width: view.frame.width, height: 44))
//        view.addSubview(toolbar)
//        var toolbarItems:[UIBarButtonItem] = []
//        toolbarItems.append(UIBarButtonItem(barButtonSystemItem: .Undo, target: self, action: ""))
//        toolbarItems.append(UIBarButtonItem(barButtonSystemItem: .Redo, target: self, action: ""))
//        toolbarItems.append(UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: ""))
//        
//        toolbar.setItems(toolbarItems, animated: true)
        
//        backToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 128, height: 44))
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "navbar_back"), style: .Plain, target: self, action: #selector(HelpWebViewController.backBtnClicked))
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        progressBar.setProgress(0.1, animated: true)
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        progressBar.setProgress(0.9, animated: true)
        progressBar.alpha = 0
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
//        progressBar.progress = 1
//        progressBar.alpha = 0
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func backBtnClicked(){
        
        if webView.canGoBack{
            webView.goBack()
            var backBtnItems:[UIBarButtonItem] = []
            backBtnItems.append(UIBarButtonItem(image: UIImage(named: "navbar_back"), style: .Plain, target: self, action: #selector(HelpWebViewController.backBtnClicked)))
            backBtnItems.append(UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: #selector(HelpWebViewController.closeBtnClicked)))
            navigationItem.leftBarButtonItems = backBtnItems
        }else{
            navigationController?.popViewControllerAnimated(true)
        }
        
        
    }
    
    func closeBtnClicked(){
        navigationController?.popViewControllerAnimated(true)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
